package com.ridaizu.bean;

import java.io.Serializable;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import org.primefaces.context.RequestContext;
import org.primefaces.event.SelectEvent;
import org.primefaces.model.LazyDataModel;

import com.google.common.base.Strings;
import com.ridaizu.cache.CacheDataBean;
import com.ridaizu.dao.db.DBManager;
import com.ridaizu.lazy.LazyGoodsCatalogDataModel;
import com.ridaizu.model.GoodsCatalog;
import com.ridaizu.model.form.GoodsCatalogForm;
import com.ridaizu.model.search.GoodsCatalogSearch;
import com.ridaizu.service.impl.GoodsCatalogServiceImpl;
import com.ridaizu.util.DateUtil;

@javax.faces.bean.ManagedBean(name = "goodsCatalogBean")
@SessionScoped
public class GoodsCatalogBean implements Serializable {

	private static final long serialVersionUID = 1L;

	private LazyDataModel<GoodsCatalog> lazyModel;

    @ManagedProperty("#{cacheDataBean}")
    private CacheDataBean cacheDataBean;

    @ManagedProperty("#{goodsCatalogServiceImpl}")
    private GoodsCatalogServiceImpl goodsCatalogService;

    @ManagedProperty("#{goodsCatalogSearch}")
    private GoodsCatalogSearch goodsCatalogSearch;

    @ManagedProperty("#{goodsCatalogForm}")
    private GoodsCatalogForm goodsCatalogForm;

	public void searchGoodsCatalogSync() {
		lazyModel = new LazyGoodsCatalogDataModel(goodsCatalogSearch);
	}

	public void viewRegisterGoodsCatalog() throws SQLException {

		setInitValues(null);

        Map<String,Object> options = new HashMap<String, Object>();
        options.put("modal", true);
        options.put("contentWidth", 550);
        options.put("contentHeight", 200);

        RequestContext.getCurrentInstance().openDialog("goods-catalog", options, null);
    }

	public void viewUpdateGoodsCatalog(GoodsCatalog goodsCatalog) throws SQLException {

		setInitValues(goodsCatalog);

        Map<String,Object> options = new HashMap<String, Object>();
        options.put("modal", true);
        options.put("contentWidth", 550);
        options.put("contentHeight", 220);

        RequestContext.getCurrentInstance().openDialog("goods-catalog", options, null);
    }

	private void setInitValues(GoodsCatalog goodsCatalog) throws SQLException {
		goodsCatalogForm.clearAll();
		setValues(goodsCatalog);
	}

	private void setValues(GoodsCatalog goodsCatalog) {
		if (goodsCatalog == null) {
			setRegisterValues();
		} else {
			setUpdateValues(goodsCatalog);
		}
	}

	private void setRegisterValues() {
	}

	private void setUpdateValues(GoodsCatalog goodsCatalog) {
		goodsCatalogForm.setId(String.valueOf(goodsCatalog.getId()));
		goodsCatalogForm.setName(goodsCatalog.getName());
		goodsCatalogForm.setDelFlg(goodsCatalog.isDelFlg());
		goodsCatalogForm.setRgtOpt(goodsCatalog.getRgtOpt());
		goodsCatalogForm.setRgtDate(new Date(goodsCatalog.getRgtDate().getTime()));
		goodsCatalogForm.setUpdOpt(goodsCatalog.getUpdOpt());
		goodsCatalogForm.setUpdDate(new Date(goodsCatalog.getUpdDate().getTime()));
	}

	public void registerGoodsCatalog() {
		GoodsCatalog goodsCatalog = getGoodsCatalog();

		boolean result = false;

		try {
			DBManager.beginTransaction();
			result = goodsCatalogService.insertModel(goodsCatalog);
			DBManager.commitTransaction();

			cacheDataBean.setGoodsCatalogSelectItemList(null);
		} catch (SQLException e) {
			e.printStackTrace();
			result = false;
			DBManager.rollbackTransaction();
		}

		RequestContext.getCurrentInstance().closeDialog(result);
    }

	public void updateGoodsCatalog() {

		GoodsCatalog goodsCatalog = getGoodsCatalog();

		boolean result = false;

		try {
			DBManager.beginTransaction();
			result = goodsCatalogService.updateModel(goodsCatalog);
			DBManager.commitTransaction();

			cacheDataBean.setGoodsCatalogSelectItemList(null);
		} catch (SQLException e) {
			e.printStackTrace();
			result = false;
			DBManager.rollbackTransaction();
		}

		RequestContext.getCurrentInstance().closeDialog(result);
	}

	public void deleteGoodsCatalog() {

		GoodsCatalog selectedGoodsCatalog = goodsCatalogForm.getSelectedGoodsCatalog() ;

		if (selectedGoodsCatalog == null || selectedGoodsCatalog.getId() < 1) {
	        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Message", "请选择要删除的商品目录信息。");
	        FacesContext.getCurrentInstance().addMessage(null, message);

	        return ;
		}

		boolean result = false;
		try {
			DBManager.beginTransaction();
			result = goodsCatalogService.deleteModel(selectedGoodsCatalog.getId());
			DBManager.commitTransaction();
		} catch (SQLException e) {
			e.printStackTrace();
			result = false;
			DBManager.rollbackTransaction();
		}

		String resultStr = null;
		if (result) {
			cacheDataBean.setGoodsCatalogSelectItemList(null);
			resultStr = String.format("商品目录[%s]删除成功。", selectedGoodsCatalog.getId());
		} else {
			resultStr = String.format("商品目录[%s]删除失败。", selectedGoodsCatalog.getId());
		}

        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "GoodsCatalog delete result", resultStr);
        FacesContext.getCurrentInstance().addMessage(null, message);
	}

	public void registerGoodsCatalogResult(SelectEvent event) {
		boolean result = (Boolean) event.getObject();

		handleResult(result, "登录");
	}

	public void updateGoodsCatalogResult(SelectEvent event) {

		boolean result = (Boolean) event.getObject();

		handleResult(result, "更新");
	}

	private void handleResult(boolean result, String optStr) {

		String resultStr = null;
		if (result) {
			resultStr = String.format("商品目录信息%s成功。", optStr);
		} else {
			resultStr = String.format("商品目录信息%s失败。", optStr);;
		}

        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Message", resultStr);

        FacesContext.getCurrentInstance().addMessage(null, message);
	}

	private GoodsCatalog getGoodsCatalog() {

		GoodsCatalog goodsCatalog = new GoodsCatalog();

		if (!Strings.isNullOrEmpty(goodsCatalogForm.getId())) {
			goodsCatalog.setId(Integer.valueOf(goodsCatalogForm.getId()));
		}

		goodsCatalog.setName(goodsCatalogForm.getName());
		goodsCatalog.setDelFlg(goodsCatalogForm.isDelFlg());

		Timestamp currentDate = DateUtil.getCurrentDateTime();

		goodsCatalog.setRgtOpt("Admin");
		goodsCatalog.setRgtDate(currentDate);
		goodsCatalog.setUpdOpt("Admin");
		goodsCatalog.setUpdDate(currentDate);

		return goodsCatalog;
	}

	public LazyDataModel<GoodsCatalog> getLazyModel() {
		return lazyModel;
	}

	public void setLazyModel(LazyDataModel<GoodsCatalog> lazyModel) {
		this.lazyModel = lazyModel;
	}

	public GoodsCatalogServiceImpl getGoodsCatalogService() {
		return goodsCatalogService;
	}

	public void setGoodsCatalogService(GoodsCatalogServiceImpl goodsCatalogService) {
		this.goodsCatalogService = goodsCatalogService;
	}

	public GoodsCatalogSearch getGoodsCatalogSearch() {
		return goodsCatalogSearch;
	}

	public void setGoodsCatalogSearch(GoodsCatalogSearch goodsCatalogSearch) {
		this.goodsCatalogSearch = goodsCatalogSearch;
	}

	public GoodsCatalogForm getGoodsCatalogForm() {
		return goodsCatalogForm;
	}

	public void setGoodsCatalogForm(GoodsCatalogForm goodsCatalogForm) {
		this.goodsCatalogForm = goodsCatalogForm;
	}

	public CacheDataBean getCacheDataBean() {
		return cacheDataBean;
	}

	public void setCacheDataBean(CacheDataBean cacheDataBean) {
		this.cacheDataBean = cacheDataBean;
	}

}